#include "MapSource2.h"


MapSource2::MapSource2(void)
{
}


MapSource2::~MapSource2(void)
{
}
char a;
/*
void MapSource2::Start()
{
    pugi::xml_document doc;
    if (!doc.load_file("xgconsole.xml")) return;

    pugi::xml_node tools = doc.child("Profile").child("Tools");

    //[code_traverse_base_basic
    for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())
    {
        std::cout << "Tool:";

        for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute())
        {
            std::cout << " " << attr.name() << "=" << attr.value();
        }

        std::cout << std::endl;
    }
    //]

    std::cout << std::endl;

    //[code_traverse_base_data
    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    {
        std::cout << "Tool " << tool.attribute("Filename").value();
        std::cout << ": AllowRemote " << tool.attribute("AllowRemote").as_bool();
        std::cout << ", Timeout " << tool.attribute("Timeout").as_int();
        std::cout << ", Description '" << tool.child_value("Description") << "'\n";
    }
    //]

    std::cout << std::endl;

    //[code_traverse_base_contents
    std::cout << "Tool for *.dae generation: " << tools.find_child_by_attribute("Tool", "OutputFileMasks", "*.dae").attribute("Filename").value() << "\n";

    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    {
        std::cout << "Tool " << tool.attribute("Filename").value() << "\n";
    }
    //]
	std::cin>>a;
}
*/
string MapSource2::mcntpack(contour_t incntr)
{
	string tmpstr;
	char buf[20];
	for(int i=0;i<incntr.size();i++)
	{
		tmpstr+="("+string(itoa(incntr[i].x,buf,10))+","+string(itoa(incntr[i].y,buf,10))+")";
	}
	return tmpstr;
}

contour_t MapSource2::mcntunpack(const pugi::char_t* indata)
{
	contour_t tmp;
	string instr=string((char*)indata);
	int x0=0,y0=0;
	int stptr=0,endptr=0,elptr=0;
	while(stptr!=instr.size())
	{
		stptr=instr.find("(",stptr);
		elptr=instr.find(",",stptr);
		x0=atoi((instr.substr(stptr+1,elptr-stptr-1)).c_str());
		endptr=instr.find(")",stptr);
		y0=atoi((instr.substr(elptr+1,endptr-elptr-1)).c_str());
		tmp.add(point_t(x0,y0));
		stptr=endptr+1;
	}
	return tmp;
}

void MapSource2::cellunpack(CELL_DATA* tmpcell, const pugi::char_t* indata)
{
	string tmpstr=string((char*)indata);
	int stptr=0;
	int elptr=0;
	if (tmpstr.empty()){return;}
	while ((elptr=tmpstr.find(",",stptr))!=-1)
	{
		tmpcell->objects.push_back(stoi(tmpstr.substr(stptr,elptr-stptr)));
		stptr=elptr+1;
	}
}



int MapSource2::ReadData(map_obj* map_objects, cell_obj* cells, char* fname)
{
	MAP_OBJECT tmp;
	contour_t tmpcnt;
	CELL_DATA tmpcell;
	const pugi::char_t* tmpc;
	pugi::xml_document doc;
	if (!doc.load_file(fname)) {return -1;};
	pugi::xml_node xml_map_objs = doc.child("MapObjects");    
	for (pugi::xml_node xml_m_obj = xml_map_objs.child("M_Obj"); xml_m_obj; xml_m_obj = xml_m_obj.next_sibling("M_Obj"))
    {
		tmp.mobj_id=atoi(xml_m_obj.attribute("id").value());						//mobj_id
		strcpy(tmp.label,xml_m_obj.attribute("title").value());						//label
		tmp.mtype=(mobj_type)atoi(xml_m_obj.attribute("type").value());				//mobj_type
		for(pugi::xml_node contour = xml_m_obj.child("contour");contour;contour=contour.next_sibling("contour"))
		{
			tmpc=contour.child_value();
			tmpcnt=mcntunpack(tmpc);
			tmp.polygon.add(tmpcnt);
		}
		map_objects->insert(map_obj_pair(tmp.mobj_id,tmp));
		tmp.polygon.clear();
    }

	pugi::xml_node xml_cells = doc.child("Cells");    
	for (pugi::xml_node xml_cell = xml_cells.child("Cell"); xml_cell; xml_cell = xml_cell.next_sibling("Cell"))
    {
		tmpcell.cell_id.first=atoi(xml_cell.attribute("x").value());						//mobj_id
		tmpcell.cell_id.second=atoi(xml_cell.attribute("y").value());						//mobj_id
		cellunpack(&tmpcell,xml_cell.child_value());

		/*
		for(pugi::xml_node cell_objs = xml_cell.child("obj");cell_objs;cell_objs=cell_objs.next_sibling("obj"))
		{
			tmpcell.objects.push_back(atoi(cell_objs.attribute("id").value()));
		}
		*/
		cells->insert(cell_obj_pair(tmpcell.cell_id,tmpcell));
		tmpcell.objects.clear();
    }
	//doc.print(std::cout);
	//doc.save_file("dataout.xml");
	//std::cin>>a;
	return 0;}


int MapSource2::WriteData(map_obj* map_objects, cell_obj* cells, char* fname)
{
	char buf[100];
	string tmpc;
	pugi::char_t* s;
	pugi::xml_document doc;
    pugi::xml_node xml_map_objs = doc.append_child("MapObjects");
	pugi::xml_node xml_cell_objs=doc.append_child("Cells");
	pugi::xml_node xml_m_obj;
	pugi::xml_node xml_c_obj;
    pugi::xml_node xml_contour;
	string tmpstr;

	map_obj::iterator mItr;
	cell_obj::iterator cItr;
	for(mItr=map_objects->begin();mItr!=map_objects->end();mItr++)
	{
	    xml_m_obj=xml_map_objs.append_child("M_Obj");
		xml_m_obj.append_attribute("id")=(pugi::char_t*)itoa(mItr->second.mobj_id,buf,10);
		xml_m_obj.append_attribute("title")=(pugi::char_t*)mItr->second.label;
		xml_m_obj.append_attribute("type")=(pugi::char_t*)itoa(mItr->second.mtype,buf,10);
		for(int i=0;i<mItr->second.polygon.size();i++)
		{
			xml_contour=xml_m_obj.append_child("contour");
			tmpc=mcntpack(mItr->second.polygon[i]);
			xml_contour.append_child(pugi::node_pcdata).set_value((pugi::char_t*)mcntpack(mItr->second.polygon[i]).c_str());
		}
	}
	for(cItr=cells->begin();cItr!=cells->end();cItr++)
	{
		xml_c_obj=xml_cell_objs.append_child("Cell");
		//xml_c_obj.append_attribute("x")=(pugi::char_t*)itoa(cItr->first.first,buf,10);
		//xml_c_obj.append_attribute("y")=(pugi::char_t*)itoa(cItr->first.second,buf,10);
		xml_c_obj.append_attribute("x")=(pugi::char_t*)itoa(cItr->first.first,buf,10);
		xml_c_obj.append_attribute("y")=(pugi::char_t*)itoa(cItr->first.second,buf,10);
		tmpstr="";
		for(int i=0;i<cItr->second.objects.size();i++)
		{
			tmpstr+=string(itoa(cItr->second.objects[i],buf,10))+",";
		}
		xml_c_obj.append_child(pugi::node_pcdata).set_value((pugi::char_t*)tmpstr.c_str());
	}

	doc.save_file(fname);
	return 0;
}
